package com.fishsticks.model;

import junit.framework.Assert;
import android.content.Intent;
import android.test.ActivityInstrumentationTestCase2;
import android.test.TouchUtils;
import android.test.UiThreadTest;
import android.widget.Button;
import android.widget.EditText;

import com.fishsticks.R;
import com.fishsticks.view.RegisterActivity;

public class RegisterActivityTest extends ActivityInstrumentationTestCase2<RegisterActivity>{
	
	private RegisterActivity ra;
	private EditText username, password, firstname, lastname, reenter;
	private Button b;
	
	public RegisterActivityTest() {
		super(RegisterActivity.class);
	}
	
	protected void setUp() throws Exception{
		super.setUp();
		Intent i = new Intent();
		setActivityIntent(i);
		ra = getActivity();
	}
	
	
	protected void tearDown() throws Exception {
		ra.finish();
		super.tearDown();
	}
	
	
	/**
	 * Test to make sure that you cannot login without all fields filled in.
	 */
	public void testEmptyRegister() {
		username = (EditText) ra.findViewById(R.id.usernameFieldRegisterPage);
		password = (EditText) ra.findViewById(R.id.passwordFieldRegisterPage);
		firstname = (EditText) ra.findViewById(R.id.firstnameFieldRegisterPage);
		lastname = (EditText) ra.findViewById(R.id.lastnameFieldRegisterPage);
		b = (Button) ra.findViewById(R.id.registerButtonRegisterPage);
		Assert.assertNotNull(username);
		username.clearComposingText();
		Assert.assertNotNull(password);
		password.clearComposingText();
		Assert.assertNotNull(firstname);
		firstname.clearComposingText();
		Assert.assertNotNull(lastname);
		lastname.clearComposingText();
		TouchUtils.tapView(this, b);
		Assert.assertNotNull(ra);
		Assert.assertNotNull(b);
		ra.runOnUiThread(new Runnable() {
			  @Override
			  public void run() {
			    b.performClick();
			  }
			});		
		Assert.assertFalse(ra.test);
	}

	/**
	 * Test to make sure that you can login with all fields filled in.
	 */
	public void testValidRegister() {
		username = (EditText) ra.findViewById(R.id.usernameFieldRegisterPage);
		password = (EditText) ra.findViewById(R.id.passwordFieldRegisterPage);
		firstname = (EditText) ra.findViewById(R.id.firstnameFieldRegisterPage);
		lastname = (EditText) ra.findViewById(R.id.lastnameFieldRegisterPage);
		reenter = (EditText) ra.findViewById(R.id.reenterPasswordFieldRegisterPage);
		b = (Button) ra.findViewById(R.id.registerButtonRegisterPage);
		TouchUtils.tapView(this, firstname);
		sendKeys("M I C H A E L");
		TouchUtils.tapView(this, lastname);
		sendKeys("D E N N E Y");		
		TouchUtils.tapView(this, username);
		sendKeys("M D E N N E Y");
		TouchUtils.tapView(this, password);
		sendKeys("1 2 3 4 5");
		TouchUtils.tapView(this, reenter);
		sendKeys("1 2 3 4 5");
		TouchUtils.tapView(this, b);
		Assert.assertNotNull(b);
		ra.runOnUiThread(new Runnable() {
			  @Override
			  public void run() {
			    b.performClick();
			  }
			});	
		Assert.assertTrue(ra.test);	
	}

	
	public void testInvalidRegister() {
		username = (EditText) ra.findViewById(R.id.usernameFieldRegisterPage);
		password = (EditText) ra.findViewById(R.id.passwordFieldRegisterPage);
		firstname = (EditText) ra.findViewById(R.id.firstnameFieldRegisterPage);
		lastname = (EditText) ra.findViewById(R.id.lastnameFieldRegisterPage);
		reenter = (EditText) ra.findViewById(R.id.reenterPasswordFieldRegisterPage);
		b = (Button) ra.findViewById(R.id.registerButtonRegisterPage);
		TouchUtils.tapView(this, firstname);
		sendKeys("M I C H A E L");
		TouchUtils.tapView(this, lastname);
		sendKeys("D E N N E Y");		
		TouchUtils.tapView(this, username);
		sendKeys("M D E N N E Y");
		TouchUtils.tapView(this, password);
		sendKeys("1 2 3 4 5");
		TouchUtils.tapView(this, reenter);
		sendKeys("W R O N G");
		TouchUtils.tapView(this, b);
		Assert.assertNotNull(b);
		ra.runOnUiThread(new Runnable() {
			  @Override
			  public void run() {
			    b.performClick();
			  }
			});	
		Assert.assertFalse(ra.test);	
	}
}